<?php

/**

 * Joomla! 1.5 component Folder Space Quota

 *

 * @version $Id: folderspacequota.php 2011-01-24 10:48:13 svn $

 * @author DoggyDams

 * @package Joomla

 * @subpackage Folder Space Quota

 * @license GNU/GPL

 *

 * Control used space of specific directory

 *

* @Copyright Copyright (C) 2010 - DoggyDamss
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html 


 *

 */



// no direct access
defined('_JEXEC') or die('Restricted access');
JHTML::_('behavior.modal');

echo '<script type="text/javascript">
function toggledir(id){
if (document.getElementById(id).style.display == "none") document.getElementById(id).style.display = "" ;
else document.getElementById(id).style.display = "none";
} </script>';

function file_size($size) {
$filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i] : '0 Byte';
}

function list_dir($dir_handle,$path,$prefix) {

$badstrings = array("../","./");
$realpath = str_replace($badstrings,"",$path);

$params = &JComponentHelper::getParams( 'com_folderspacequota' );
$addsub = $params->get( 'addsubtree' );
$subprefix = $params->get( 'subfolderprefix' );

$files = array();
$folders = array();
	while (false !== ($file = readdir($dir_handle))) {
	$dir =$path.'/'.$file;
		if ($file != '.' && $file !='..') {
			if (is_dir($dir)) $folders[] = $file;  else $files[] = $file;
		}
	sort ($files); reset ($files);
	sort ($folders); reset ($folders);
	}

	if ($files != NULL) {
		foreach ($files as $file) {
		$dir = $path.'/'.$file;
		$size = filesize($dir);
		echo '<div style="padding-left:30px;"><a class="modal" rel="{handler: \'iframe\', size: {x: 640, y: 480}}" href="http://'.$_SERVER['SERVER_NAME'].'/'.$realpath.'/'.$file.'">'.$file.' <i>('.file_size($size).')</i></a><br/></div>';
		}
	}
	else echo '<div style="font-size:12px;">'.JText::_( 'NO_FILE').'</div>';
	
echo "<br/>";

	if($folders != NULL) {
	echo "<br/><ul>";
		foreach ($folders as $file) {
    	$dir =$path.'/'.$file;
    	$handle = @opendir($dir) or die("undable to open file $file");
    	echo "<li style='font-size:12px;font-weight:bold;text-decoration:underline;'><a href='javascript:void();' onclick='toggledir(\"".$realpath."/".$file."\");' >".$prefix.$file."</a></li><br/>";
			
			echo "<div id='".$realpath."/".$file."' style='display:none;'>";
			if ($addsub == 1 ) {list_dir($handle, $dir, $subprefix);
			} else echo JText::_( 'CONF_NOSUB') ;
			echo "</div>";
			
		echo "<br/>";
		}
	echo "</ul>";
	}
	else echo '<div style="font-size:12px;">'.JText::_( 'NO_FOLDER').'</div><br/>';
closedir($dir_handle);
}

$imgPath4 = JRoute::_('/administrator/components/com_folderspacequota/icons/quotatree.png');
$imgPath3 = JRoute::_('/administrator/components/com_folderspacequota/icons/home.png');
$imgPath2 = JRoute::_('/administrator/components/com_folderspacequota/icons/quotaf32.png');
$imgPath = JRoute::_('/administrator/components/com_folderspacequota/icons/treefold32.png');
JFactory::getDocument()->addStyleDeclaration(".icon-32-treefold { background: url($imgPath); }.icon-32-quotaf { background: url($imgPath2); }.icon-32-home { background: url($imgPath3); }.icon-48-quotatree { background: url($imgPath4); }");

JToolBarHelper::title(JText::_('Folder Space Quota'), 'quotatree.png');

$bar = & JToolBar::getInstance('toolbar');

$url3 = JRoute::_('index.php?option=com_folderspacequota&task=default&view=default');
$alt3 = JText::_( 'QUOTA_HOME');
$bar->appendButton( 'Link', 'home', $alt3, $url3);

$url2 = JRoute::_('index.php?option=com_folderspacequota&task=default&view=quota');
$alt2 = JText::_( 'QUOTA_QUOTAS');
$bar->appendButton( 'Link', 'quotaf', $alt2, $url2);

$url = JRoute::_('index.php?option=com_folderspacequota&task=default&view=list');
$alt = JText::_( 'QUOTA_CONTENTS');
$bar->appendButton( 'Link', 'treefold', $alt, $url);

JToolBarHelper::preferences('com_folderspacequota',$height='425');

$params = &JComponentHelper::getParams( 'com_folderspacequota' );
$path = $params->get( 'foldertoscan' );
$foldprefix = $params->get( 'folderprefix' );

if ($path = $params->get( 'foldertoscan' )){
$badstrings = array("../","./");
$realpath = str_replace($badstrings,"",$path);
	if (substr($realpath,0,1) == '/') $realpath = substr($realpath,1) ;
	if ($dir_handle = @opendir("../".$realpath)) {
	echo '<br/><div><span style="font-size:14px;font-weight:bold;">'.JText::_( 'PATH_DIR').' : '.$realpath.'</span> <span style="font-size:12px;">&nbsp;&nbsp;(&lt;root&gt; <i>/'.$realpath.'</i>)</span></div><br/>'; list_dir($dir_handle,"../".$realpath,$foldprefix);
	} else echo '<br/><div style="font-size:14px;text-align:center;"><strong>'.JText::_( 'PATH_ERROR').'</strong><br/>'.JText::_( 'PATH_ERROR_').'</div><br/>';
	
} else echo '<br/><div style="font-size:14px;text-align:center;"><strong>'.JText::_( 'CONF_ERROR').'</strong><br/>'.JText::_( 'CONF_ERROR_').'</div><br/>';



?>
